package uk.ac.manchester.tewpl.iceiface.client.explorer.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;

import uk.ac.manchester.tewpl.iceiface.client.Functors.VoidCallback;
import uk.ac.manchester.tewpl.iceiface.client.explorer.Utils;
import uk.ac.manchester.tewpl.iceiface.client.explorer.events.ExplorerEvent;
import uk.ac.manchester.tewpl.iceiface.client.explorer.nodes.Airport;
import uk.ac.manchester.tewpl.iceiface.client.explorer.types.Mutable;
import uk.ac.manchester.tewpl.iceiface.client.explorer.types.Route;
import uk.ac.manchester.tewpl.iceiface.client.model.KBIndividualModel;

public class StatusPanel extends UI {
	
	// Retrieve variables
	private Collection<Airport> airports = Registry.get("airports");
	private Mutable<Airport> airportSelected = Registry.get("airportSelected");
	private LinkedList<Airport> journey = Registry.get("journey");
	private ArrayList<KBIndividualModel> timePeriods = Registry.get("timePeriods");
	
	private Html tAirports = new Html();
	private Html tAirportSelected = new Html();
	private Html tAirportSelectionList  = new Html();
	//private Html tRoutes = new Html();
	private Html tTimePeriods = new Html();
	private Html tEvent = new Html();
	

	@Override
	public void init(VoidCallback onFinishCallback) {
		this.setLayout(new FlowLayout());
		this.setHeaderVisible(false);
		this.add(tAirports);
		this.add(tAirportSelected);
		this.add(tAirportSelectionList);
		//this.add(tRoutes);
		this.add(tTimePeriods);
		this.add(tEvent);
		
		
		onFinishCallback.work();
		
	}

	@Override
	public void handle(ExplorerEvent event) {
		tAirports.setHtml("Airports:<br>" + collectionToString(airports) + "<br>");
		tAirportSelected.setHtml("Airport selected:<br>" + airportSelectedDetails() + "<br>");
		tAirportSelectionList.setHtml("Journey:<br>" + collectionToString(journey) + "<br>");
		//tRoutes.setHtml("Possible routes:<br>" + collectionToString(routes) + "<br>");
		tTimePeriods.setHtml("Time Periods:<br>" + collectionToString(timePeriods) + "<br>");
		String [] classPath = event.getClass().toString().split("\\.");
		String className = classPath[classPath.length - 1];
		tEvent.setHtml("Last event:<br>" + className + "<br>");
		this.layout();
	}
	
	private String collectionToString(Collection objects) {
		if (objects == null)
			return "null<br>";
		if (objects.size() == 0)
			return "empty<br>";
		
		StringBuffer sb = new StringBuffer();
		for (Object obj : objects) {
			sb.append ("&bull; " + obj.toString() + "<br>");
		}
		return sb.toString();
	}
	
	private String airportSelectedDetails() {
		Airport airport = airportSelected.get();
		if (airport == null)
			return "null<br>";
		
		StringBuffer sb = new StringBuffer();
		sb.append("&bull; " + airport.getLabel() + "<br>");
		Airport prev = airport.getPreviousInJourney();
		Airport next = airport.getNextInJourney();
		sb.append(" <- " + (prev != null ? prev.getLabel() : "null") + "<br>");
		sb.append(" -> " + (next != null ? next.getLabel() : "null") + "<br>");
		if (airport.getFilterKeywords().size() > 0) {
			sb.append("  Filter keywords:<br>");
			for (String keyword : airport.getFilterKeywords())
				sb.append("  &bull; " + keyword + "<br>");
		}
		return sb.toString();
	}

}
